package com.zxh.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.zxh.dao.ResumeDao;
import com.zxh.entity.ResumeBasicInfo;
@WebServlet("/resumeupload.do")
@MultipartConfig  //标示servlet支持文件上传
public class ResumeBasicInfoPicUpload extends HttpServlet{
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			doPost(req, resp);
		}
		
		
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//获取上传的文件域
			Part part  = req.getPart("headShot");
			String header  = part.getHeader("content-disposition");//获取内容的头信息
			String fileName = header.substring(header.indexOf("fileName=\"")+10,header.lastIndexOf("\""));//获取上传的文件名
			System.out.println("刚上传的文件名为："+fileName);
			String newFileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));//产生一个新的文件名
			System.out.println("新的文件名："+newFileName);
			String filepath = getServletContext().getRealPath("applicant/images");//获取上传文件的保存路径，这个路径发布在项目服务器的某个目录下
			System.out.println(filepath);
			File  f = new File(filepath);
			if(!f.exists()){
				//如果目录不存在就创建一个
				f.mkdirs();
			}
			part.write(filepath+"/"+newFileName);//上传文件
			//把图片的文件名存在数据库中
			ResumeDao dao = new ResumeDao();
			HttpSession session =req.getSession();
			int basicinfoId  = (Integer) session.getAttribute("basicinfoId");
			System.out.println(basicinfoId);
			dao.updateHeadShot(basicinfoId, newFileName);
			ResumeBasicInfo resume = dao.queryResuemBasicInfo(basicinfoId);
			session.setAttribute("basicinfoId", resume.getBasicinfoId());//把简历信息id存在session中
			req.setAttribute("resuemInfo", resume);
			req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp);
			
		}
}
